package com.zhiyou100.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zhiyou100.model.ResultObject;
import com.zhiyou100.model.User;
import com.zhiyou100.service.LoginService;
import com.zhiyou100.util.MyLogAnno;

@Controller
public class UserController {
	
	@Autowired
	private LoginService ls;
	//注册用户

	@MyLogAnno("用户注册")
	@RequestMapping("/entrance/reg.jsp")
	public String loginUser() {
		System.out.println("进入/user/add=============>>");
		return "forward:/view/reg.jsp";
	}
	@MyLogAnno("用户注册提交")
	@RequestMapping("/login/add")
	public String loginUser2(User user,String name,String password,Model model,String ipone) {
		System.out.println("进入/login/add================>>");
		//查找用户
		User user1 = ls.findUserByName(name);
		System.out.println(user1);
		
		if(user1 == null || ipone == null) {
			//用户为空添加用户
			ls.insertUser(user);
			System.out.println(user);
			if(name != null || name != ""&& password != null || password != ""&& ipone != null || ipone !="") {
				
					System.out.println("注册成功");
					return "forward:/login.jsp";
			}
		}else {
			System.out.println("用户名或手机号已存在 请重新输入");
		}
		
		return "forward:/view/reg.jsp";
	}
	
	
	//登录
	@MyLogAnno("用户登录")
	@RequestMapping("/findUserByName")
	public String findUserByIdName(String un,String pwd,Model model,HttpServletRequest request) {
		System.out.println("进入findUserByName ===+++");
		System.out.println("un: "+un);
		Map<String,String> map = new HashMap<String,String>();
		map.put("name", un);
		map.put("password", pwd);
		System.out.println("map: "+map);
		User user = ls.findUser(map);
		request.getSession().setAttribute("user",user);
		System.out.println("user: "+user);
		model.addAttribute("user", user);
		if(un.equals(user.getName())&& pwd.equals(user.getPassword())) {
			System.out.println("登录成功");
			return "forward:/index.jsp";
		}
		model.addAttribute("msg", "账号或密码错误");
		return "forward:/login.jsp";
		
	}
	//个人信息查询
	@RequestMapping("/addUserById")
	public String addUserById(User user) {
		Map<String,Object> map = new HashMap<>();
		map.put("user", user);
		ls.findUserById(user);
		return "forward:/view/new_product.jsp";
		
	}
}
